package com.lw.lost.activity

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.lw.lost.MainActivity
import com.lw.lost.databinding.ActivityLoginBinding
import com.lw.lost.db.DBlost
import com.lw.lost.utils.PreferenceUtils

class LoginActivity : AppCompatActivity() {
    
    private lateinit var binding: ActivityLoginBinding
    private lateinit var db: DBlost

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)

        db = DBlost(this)
        initViews()
    }

    private fun initViews() {
        // 登录按钮点击事件
        binding.btnLogin.setOnClickListener {
            val username = binding.etUsername.text.toString()
            val password = binding.etPassword.text.toString()

            if (username.isEmpty()) {
                binding.tilUsername.error = "请输入用户名"
                return@setOnClickListener
            }

            if (password.isEmpty()) {
                binding.tilPassword.error = "请输入密码"
                return@setOnClickListener
            }

            // 验证登录
            val cursor = db.userDao.login(username, password)
            if (cursor.moveToFirst()) {
                val status = cursor.getInt(cursor.getColumnIndexOrThrow("status"))
                val userId = cursor.getLong(cursor.getColumnIndexOrThrow("userId"))
                val name = cursor.getString(cursor.getColumnIndexOrThrow("name"))
                
                when (status) {
                    0 -> {
                        Toast.makeText(this, "账号已被封禁", Toast.LENGTH_SHORT).show()
                    }
                    1 -> {
                        // 普通用户登录
                        saveUserInfo(userId, name, false)
                        startActivity(Intent(this, MainActivity::class.java))
                        finish()
                    }
                    2 -> {
                        // 超级管理员登录
                        saveUserInfo(userId, name, true)
                        startActivity(Intent(this, AdminActivity::class.java))
                        finish()
                    }
                }
            } else {
                Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show()
            }
            cursor.close()
        }

        // 注册按钮点击事件
        binding.btnRegister.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
        }

        // 微信登录点击事件
        binding.ivWechat.setOnClickListener {
            Toast.makeText(this, "微信登录", Toast.LENGTH_SHORT).show()
        }

        // QQ登录点击事件
        binding.ivQQ.setOnClickListener {
            Toast.makeText(this, "QQ登录", Toast.LENGTH_SHORT).show()
        }
    }

    private fun saveUserInfo(userId: Long, name: String, isAdmin: Boolean) {
        PreferenceUtils.saveUserInfo(this, userId, name, isAdmin)
    }
} 